package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.admin.AddressListBO;
import com.wangdao.wechatmall.bean.pojo.RegionExample;
import com.wangdao.wechatmall.bean.vo.wx.WXAddressVO;
import com.wangdao.wechatmall.bean.pojo.Address;
import com.wangdao.wechatmall.bean.pojo.AddressExample;
import com.wangdao.wechatmall.bean.pojo.Region;
import com.wangdao.wechatmall.bean.vo.admin.AddressLIstVO;
import com.wangdao.wechatmall.bean.vo.admin.BaseDataVO;
import com.wangdao.wechatmall.bean.vo.wx.WXAddressDetailVO;
import com.wangdao.wechatmall.bean.vo.wx.WXAddressListVO;
import com.wangdao.wechatmall.mapper.AddressMapper;
import com.wangdao.wechatmall.mapper.AdminMapper;
import com.wangdao.wechatmall.mapper.RegionMapper;
import com.wangdao.wechatmall.mapper.UserMapper;
import com.wangdao.wechatmall.util.MyPageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @author ZhangXiao on 2021/7/9
 */
@Service
public class AddressServiceImpl implements AddressService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public BaseDataVO<AddressLIstVO> queryAddress(AddressListBO addressListBO) {
        MyPageHelper.startPage(addressListBO);
        if (addressListBO.getName() != null && !"".equals(addressListBO.getName())) {
            String newName = "%" + addressListBO.getName() + "%";
            addressListBO.setName(newName);
        }


        List<AddressLIstVO> addressLIstVOList = adminMapper.selectAddress(addressListBO);

        PageInfo<AddressLIstVO> addressLIstVOPageInfo = new PageInfo<>(addressLIstVOList);
        long total = addressLIstVOPageInfo.getTotal();

        return BaseDataVO.ok(addressLIstVOList, total);
    }


    @Autowired
    AddressMapper addressMapper;

    @Autowired
    RegionMapper regionMapper;

    @Autowired
    UserMapper userMapper;

    /**
     * WXaddressListVO
     * created by feng on 7/11/21
     */
    @Override
    public List<WXAddressListVO> list() {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();

        String username = (String)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        int userid = userMapper.queryId(username);
        criteria.andUserIdEqualTo(userid);


        // 未删除
        criteria.andDeletedEqualTo(false);

        List<Address> addresses = addressMapper.selectByExample(addressExample);

        // return List<WXAddressVO>
        List<WXAddressListVO> wxAddressVOList = new ArrayList<>();


        Iterator<Address> iterator = addresses.iterator();
        while (iterator.hasNext()) {
            // get address
            Address address = (Address) iterator.next();

            // set address
            WXAddressListVO wxAddressVO = new WXAddressListVO();

            // find province
            Region province = regionMapper.selectByPrimaryKey(address.getProvinceId());

            // find city
            Region city = regionMapper.selectByPrimaryKey(address.getCityId());

            // find area
            Region area = regionMapper.selectByPrimaryKey(address.getAreaId());

            // combine province, city and area
            String addressPre = province.getName() + city.getName() + area.getName();

            wxAddressVO.setDetailedAddress(addressPre + " " + address.getAddress());
            wxAddressVO.setId(address.getId());
            wxAddressVO.setIsDefault(address.getIsDefault());
            wxAddressVO.setName(address.getName());
            wxAddressVO.setMobile(address.getMobile());

            // add a wxAddressVo to wxAddressVOList
            wxAddressVOList.add(wxAddressVO);

        }

        // return wxAddressVOList
        return wxAddressVOList;

    }

    /**
     * WXaddressDeatil
     * created by feng on 7/11/21
     */
    @Override
    public WXAddressDetailVO detail(Integer id) {

        Address address = addressMapper.selectByPrimaryKey(id);

        // new WXAddressVO
        WXAddressDetailVO wxAddressDetailVO = new WXAddressDetailVO();

        // find province
        Region province = regionMapper.selectByPrimaryKey(address.getProvinceId());

        // find city
        Region city = regionMapper.selectByPrimaryKey(address.getCityId());

        // find area
        Region area = regionMapper.selectByPrimaryKey(address.getAreaId());


        // set wxAddressDetailVO
        wxAddressDetailVO.setAddress(address.getAddress());
        wxAddressDetailVO.setProvinceId(address.getProvinceId());
        wxAddressDetailVO.setId(address.getId());
        wxAddressDetailVO.setMobile(address.getMobile());
        wxAddressDetailVO.setCityName(city.getName());
        wxAddressDetailVO.setName(address.getName());
        wxAddressDetailVO.setIsDefault(address.getIsDefault());
        wxAddressDetailVO.setCityId(address.getCityId());
        wxAddressDetailVO.setAreaName(area.getName());
        wxAddressDetailVO.setAreaId(address.getAreaId());
        wxAddressDetailVO.setProvinceName(province.getName());


        return wxAddressDetailVO;
    }

    @Override
    public Integer update(WXAddressVO wxAddressVO) {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        // the same addressId
        criteria.andIdEqualTo(wxAddressVO.getId());

        // create Address
        Date date = new Date();
        Address address = new Address();
        address.setAddress(wxAddressVO.getAddress());
        address.setAreaId(wxAddressVO.getAreaId());
        address.setCityId(wxAddressVO.getCityId());
        address.setMobile(wxAddressVO.getMobile());
        address.setName(wxAddressVO.getName());
        address.setProvinceId(wxAddressVO.getProvinceId());
        address.setUpdateTime(date);
        address.setDeleted(false);
        address.setIsDefault(wxAddressVO.getIsDefault());

        //set default
        if (wxAddressVO.getIsDefault() == true) {
            Address addressDefault = new Address();
            addressDefault.setIsDefault(false);

            AddressExample addressExampleDefault = new AddressExample();
            AddressExample.Criteria criteriaDefault = addressExample.createCriteria();
            // userId and idDefault
            criteriaDefault.andUserIdEqualTo(wxAddressVO.getId());
            criteriaDefault.andIsDefaultEqualTo(true);

            addressMapper.updateByExampleSelective(addressDefault, addressExampleDefault);
        }

        //update
        int affectedRow = addressMapper.updateByExampleSelective(address, addressExample);


        return affectedRow;
    }

    @Override
    public Integer delete(Integer id) {
        // create address
        Address address = new Address();
        address.setDeleted(true);
        address.setId(id);

        int affectedRow = addressMapper.updateByPrimaryKeySelective(address);

        return affectedRow;
    }

    @Override
    public Integer add(WXAddressVO wxAddressVO) {

        // create Address
        Address address = new Address();

        Date date = new Date();

        //update Default
        if (wxAddressVO.getIsDefault() == true) {
            Address addressDefault = new Address();
            addressDefault.setIsDefault(false);

            AddressExample addressExample = new AddressExample();
            AddressExample.Criteria criteria = addressExample.createCriteria();
            // userId and idDefault
            String username = (String)SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
            int userid = userMapper.queryId(username);
            criteria.andUserIdEqualTo(userid);
            criteria.andIsDefaultEqualTo(true);

            addressMapper.updateByExampleSelective(addressDefault, addressExample);
        }

        address.setUserId(1);
        address.setAddress(wxAddressVO.getAddress());
        address.setAreaId(wxAddressVO.getAreaId());
        address.setCityId(wxAddressVO.getCityId());
        address.setMobile(wxAddressVO.getMobile());
        address.setName(wxAddressVO.getName());
        address.setProvinceId(wxAddressVO.getProvinceId());
        address.setIsDefault(wxAddressVO.getIsDefault());
        address.setAddTime(date);
        address.setUpdateTime(date);
        address.setIsDefault(wxAddressVO.getIsDefault());

        //insert
        addressMapper.insertSelective(address);
        Integer insertedAddressId = address.getId();

        // return id
        return insertedAddressId;
    }
}
